package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.component.CloudService;
import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.service.WxStorageService;
import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;


@RestController
@RequestMapping("wx/storage")
public class WxStorageController {
    @Autowired
    WxStorageService wxStorageService;


    @Autowired
    CloudService cloudService;

    @PostMapping("upload")
    public Object upload(@RequestParam("file") MultipartFile file) throws IOException {

        InputStream inputStream = file.getInputStream();
        String filename = file.getOriginalFilename();
        filename = UUID.randomUUID().toString() + ".png";
        String path = cloudService.oss(inputStream, filename);

        int i = path.lastIndexOf("/");
        String key=path.substring(i+1);
//        System.out.println(path);


//        MarketStorage marketStorage=wxStorageService.upload(file);
        MarketStorage marketStorage=wxStorageService.uploadAliyun(file,key);
        Object ok = ResponseUtil.ok(marketStorage);
        return ok;
    }
}
